/*
 * Copyright 2015 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package com.zhongan.gateway.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zhongan.gateway.bean.Api;
import com.zhongan.gateway.bean.ApiAttribute;
import com.zhongan.gateway.bean.ApiGroup;
import com.zhongan.gateway.common.constant.GwDeveloperApiConstant;
import com.zhongan.gateway.common.enums.ResponseCodeEnum;
import com.zhongan.gateway.common.exception.GatewayException;
import com.zhongan.gateway.service.ApiService;
import com.zhongan.gateway.web.common.BaseController;
import com.zhongan.gateway.web.common.PageDTO;
import com.zhongan.gateway.web.common.ResultBase;

/***
 * 测试用的Controller
 * 
 * @author
 */
@Controller
@RestController
@Configuration
@RequestMapping("/api")
public class ApiController extends BaseController {

    //    private Logger logger = LoggerFactory.getLogger(TestController.class);

    @Resource
    private ApiService apiService;

    @RequestMapping("/getAllGroups")
    ResultBase<Object> getAllGroups() {
        List<ApiGroup> list = apiService.selectGroups();
        String re = JSONObject.toJSONStringWithDateFormat(list, GwDeveloperApiConstant.LOG_TIME_FORMAT,
                SerializerFeature.WriteDateUseDateFormat);
        return getSucResultBase(re);
    }

    @RequestMapping("/updateGroup")
    ResultBase<Object> updateGroup(ApiGroup apiGroup) {
        int i = apiService.updateApiGroup(apiGroup);

        return getSucResultBase(i);

    }

    @RequestMapping("/insertGroup")
    ResultBase<Object> insertGroup(ApiGroup apiGroup) {
        int i = apiService.insertApiGroup(apiGroup);
        return getSucResultBase(i);
    }

    @RequestMapping("/deleteGroup")
    ResultBase<Object> deleteGroup(Long id) {
        int i = apiService.deleteGroupById(id);
        return getSucResultBase(i);
    }

    @RequestMapping("/getGroupById")
    ResultBase<Object> getGroupById(Long id) {
        ApiGroup group = apiService.selectGroupById(id);
        return getSucResultBase(group);
    }

    @RequestMapping("/getApiById")
    ResultBase<Object> getApiById(Long id) {
        Api api = apiService.selectApiById(id);
        return getSucResultBase(api);
    }

    @RequestMapping("/updateApi")
    ResultBase<Object> updateApi(Api api) {
        int i = apiService.updateApi(api);
        return getSucResultBase(i);
    }

    @RequestMapping("/insertApi")
    ResultBase<Object> insertApi(Api api) {
        Long id = apiService.insertApi(api);
        return getSucResultBase(id);
    }

    @RequestMapping("/deleteApi")
    ResultBase<Object> deleteApi(Api record) {
        int i = apiService.deleteApi(record);
        return getSucResultBase(i);
    }

    @RequestMapping("/getApis")
    PageDTO<Api> getApis(String serviceName, Long groupId, Integer start, Integer limit) throws GatewayException {
        if (StringUtils.isEmpty(serviceName) && null == groupId) {
            throw new GatewayException(ResponseCodeEnum.PARAM_FAIL.getCode(), "group和serviceName不能同时为空");
        }

        PageDTO<Api> page = apiService.selectPageApis(serviceName, groupId, start, limit);

        return page;
    }

    @RequestMapping("/getApiAttrById")
    ResultBase<Object> getApiAttrById(Long id) {
        ApiAttribute apiAttribute = apiService.selectApiAttrById(id);
        return getSucResultBase(apiAttribute);
    }

    @RequestMapping("/updateApiAttr")
    ResultBase<Object> updateApiAttr(ApiAttribute apiAttribute) {
        int i = apiService.updateApiAttr(apiAttribute);
        return getSucResultBase(i);
    }

    @RequestMapping("/insertApiAttr")
    ResultBase<Object> insertApiAttr(ApiAttribute apiAttribute) {
        int i = 0;
        if (null != apiAttribute) {
            if (null == apiAttribute.getId() || 0 == apiAttribute.getId()) {
                i = apiService.insertApiAttr(apiAttribute);
            } else {
                i = apiService.updateApiAttr(apiAttribute);
            }
        }

        return getSucResultBase(i);
    }

    @RequestMapping("/deleteApiAttr")
    ResultBase<Object> deleteApiAttr(ApiAttribute apiAttribute) {
        int i = apiService.deleteApiAttr(apiAttribute.getId(), apiAttribute.getApiId());
        return getSucResultBase(i);
    }

    @RequestMapping("/getApiAttrs")
    ResultBase<Object> getApiAttrs(Long apiId) throws GatewayException {
        if (null == apiId) {
            throw new GatewayException(ResponseCodeEnum.FAIL.getCode(), "apiId不能为空");
        }
        List<ApiAttribute> apis = apiService.selectApiAttrs(apiId);
        return getSucResultBase(apis);
    }

}
